package com.jaws.demo.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.jaws.core.common.mybatis.JawsMapperScan;
import com.jaws.core.compatible.service.impl.CacheServiceMemcachedImpl;
import com.jaws.demo.common.startup.ClientDemoStartup;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 配置类，相当于xml
 *
 * @author maojian
 * @date 2018-03-12 08:54:28
 * @since v1.0.0
 */
@Configuration
@EnableConfigurationProperties(ClientDemoConfigProperties.class)
@JawsMapperScan("${yjs.jaws.mybatis.basePackages}")
public class ClientDemoConfiguration {

	@Resource
	ApplicationContext context;


	@Bean
	@ConfigurationProperties(prefix = "spring.datasource.demo")
	@Primary
	public DataSource dataSource() {
		DruidDataSource datasource = new DruidDataSource();
		return datasource;
	}

	@Bean
	public ClientDemoStartup clientDemoStartup() {
		return new ClientDemoStartup();
	}

	//延时队列定义
	@Bean
	public Queue deadLetterQueue() {
		Map<String, Object> arguments = new HashMap<String, Object>();
		arguments.put("x-message-ttl", Long.valueOf("10000"));
		//定义死信路由
		arguments.put("x-dead-letter-exchange", "jaws.topic.exchange");
		arguments.put("x-dead-letter-routing-key", "jaws.topic.target");
		return new Queue("jaws.dead.letter", true, false, false, arguments);
	}

	@Bean
	TopicExchange topicExchange() {
		return new TopicExchange("topic.exchange", true, false);
	}

	@Bean
	Binding payDelay5Key(Queue deadLetterQueue, TopicExchange topicExchange) {
		return BindingBuilder.bind(deadLetterQueue).to(topicExchange).with("jaws.topic.message3");
	}

	@Bean
	public CacheServiceMemcachedImpl demoCacheService(ClientDemoConfigProperties clientDemoConfigProperties) {
		CacheServiceMemcachedImpl payCacheService = new CacheServiceMemcachedImpl(clientDemoConfigProperties.getCacheNamspace());
		payCacheService.setOn(clientDemoConfigProperties.getCacheSwitchOn());
		return payCacheService;
	}

//	@Bean
//    public TaskExecutor taskExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        // 设置核心线程数
//        executor.setCorePoolSize(2);
//        // 设置最大线程数
//        executor.setMaxPoolSize(3);
//        // 设置队列容量
//        executor.setQueueCapacity(5);
//        // 设置线程活跃时间（秒）
//        executor.setKeepAliveSeconds(60);
//        // 设置默认线程名称
//        executor.setThreadNamePrefix("hello-");
//        // 设置拒绝策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        // 等待所有任务结束后再关闭线程池
//        executor.setWaitForTasksToCompleteOnShutdown(true);
//        return executor;
//    }
}
